package com.hlf.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hlf.eduservice.entity.EduSubject;
import com.hlf.eduservice.entity.excel.SubjectData;
import com.hlf.eduservice.entity.subject.OneSubject;
import com.hlf.eduservice.entity.subject.TwoSubject;
import com.hlf.eduservice.listener.SubjectExcelListener;
import com.hlf.eduservice.mapper.EduSubjectMapper;
import com.hlf.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.servicebase.GlobalExceptionHandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author hlf
 * @since 2022-11-27
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    /**
     * @author hlf
     * @date 2022/11/27 16:57
     * @description 添加课程分类
     **/
    @Override
    public void saveSubject(MultipartFile file,EduSubjectService subjectService) {

        try {
            // 获取文件流
            InputStream in = file.getInputStream();
            // 读取
            EasyExcel.read(in, SubjectData.class,new SubjectExcelListener(subjectService)).sheet().doRead();
        }catch (Exception e){
            e.printStackTrace();
            throw new GuliException(20002,"添加课程分类失败");
        }

    }

    /**
     * @author hlf
     * @date 2022/11/27 21:10
     * @description 树形结构
     **/
    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        //最终要的到的数据列表
        ArrayList<OneSubject> finalSubjectList = new ArrayList<>();
        // 查一级分类
        QueryWrapper<EduSubject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", 0);
        queryWrapper.orderByAsc("sort", "id");
        List<EduSubject> oneSubjects = baseMapper.selectList(queryWrapper);

        // 查二级分类
        QueryWrapper<EduSubject> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.ne("parent_id", 0);
        queryWrapper2.orderByAsc("sort", "id");
        List<EduSubject> twoSubSubjects = baseMapper.selectList(queryWrapper2);

        //封装数据
        for (int i=0;i<oneSubjects.size();i++){
            EduSubject eduSubject = oneSubjects.get(i);
            OneSubject oneSubject = new OneSubject();
            BeanUtils.copyProperties(eduSubject, oneSubject);
            finalSubjectList.add(oneSubject);

            List<TwoSubject> twofinalSubjectList = new ArrayList<>();
            // 遍历
            for (int m = 0; m < twoSubSubjects.size(); m++) {
                EduSubject tSubject = twoSubSubjects.get(m);
                if(tSubject.getParentId().equals(eduSubject.getId())){
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(tSubject, twoSubject);
                    twofinalSubjectList.add(twoSubject);
                }
            }
            oneSubject.setChildren(twofinalSubjectList);
        }

        return finalSubjectList;
    }


}
